package com.zmht.beike.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.zmht.beike.core.BaseAction;
import com.zmht.beike.po.TIaiminfo;
import com.zmht.beike.po.TImonthinfo;
import com.zmht.beike.po.TIweekcontent;
import com.zmht.beike.po.TIweekinfo;
import com.zmht.beike.util.TimeUtil;
import com.zmht.specedu.vo.Teacher;
/**
 * 个别周主题基本记录
 * @author Administrator
 *
 */
public class IWeekinfoAction extends BaseAction {

	/*
	 * 个别周主题记录
	 * (non-Javadoc)
	 * @see org.apache.struts.actions.DispatchAction#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward initIWeekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		String mid = request.getParameter("mid");//添加个别周主题时传入
		TImonthinfo imonthinfo = this.getImonthinfoBiz().findById(new Integer(mid));
		TIaiminfo iaiminfo = this.getIaiminfoBiz().findById(imonthinfo.getIaimId());
		//System.out.println(imonthinfo.getMonthName());
		request.getSession().setAttribute("imonthinfo", imonthinfo);
		request.getSession().setAttribute("iaiminfo", iaiminfo);
		request.setAttribute("mid", mid);
		return mapping.findForward("addgebiezhouzhuti");
	}
	/**
	 * 添加周主题记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward saveIWeekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		TIaiminfo iaiminfo = (TIaiminfo)request.getSession().getAttribute("iaiminfo");
		TImonthinfo imonthinfo = (TImonthinfo)request.getSession().getAttribute("imonthinfo");
		
		//String cmonthId = request.getParameter("cmonthId");
		//String week = request.getParameter("week");
		
		 
		//获得所有周内容
		List list1 = new ArrayList();
		List list2 = new ArrayList();
		List list3 = new ArrayList();
		List list4 = new ArrayList();
		list1 = this.getIweekContentBiz().getIweekcontent(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(), 1, 0, 100);
		list2 = this.getIweekContentBiz().getIweekcontent(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(), 2, 0, 100);
		list3 = this.getIweekContentBiz().getIweekcontent(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(), 3, 0, 100);
		list4 = this.getIweekContentBiz().getIweekcontent(teacher.getTeacherId()+"",iaiminfo.getId(),imonthinfo.getMonthType(), 4, 0, 100);
		
		
		this.saveWeekInfo(list1, teacher.getTeacherId(), imonthinfo.getId(),1);
		this.saveWeekInfo(list2, teacher.getTeacherId(), imonthinfo.getId(),2);
		this.saveWeekInfo(list3, teacher.getTeacherId(), imonthinfo.getId(),3);
		this.saveWeekInfo(list4, teacher.getTeacherId(), imonthinfo.getId(),4);
		
		return new ActionForward("/beike/iweekinfo.do?method=getIWeekinfo");
	}
	private boolean saveWeekInfo(List<TIweekcontent> list,Integer teacherId,Integer imonthId,Integer week){
		if(list!=null&&list.size()>0){
			TIweekinfo weekinfo = new TIweekinfo();
			weekinfo.setWeekThemeName(list.get(0).getWeekThemeName());
			weekinfo.setTeacherId(teacherId+"");
			weekinfo.setImonthId(imonthId);
			weekinfo.setWeek(week);
			weekinfo.setCtime(TimeUtil.getYMDTime());
			weekinfo.setIaimId(list.get(0).getIaimId());
			
			Integer id = this.getIweekinfoBiz().saveIWeekInfo(weekinfo);
			for(TIweekcontent t:list){
				t.setIweekId(id);
				t.setImonthId(imonthId);
				this.getIweekContentBiz().updateIWeekContent(t);
			}
			
		}
		return true;
	}
	/**
	 * 获得周主题记录 带分页
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getIWeekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		Teacher teacher = (Teacher)request.getSession().getAttribute("current_user_session_key");
		
		Integer index = 1;
		Integer pageSize = 5;
		if (null != request.getParameter("currPage")) {
			index = new Integer(request.getParameter("currPage"));
		}
		if (null != request.getParameter("pageSize")) {
			pageSize = new Integer(request.getParameter("pageSize"));
		}
		int total = this.getIweekinfoBiz().getIWeekInfoTotal(teacher.getTeacherId());
		List<TIweekinfo> list = this.getIweekinfoBiz().getIWeekInfo(teacher.getTeacherId(), (index-1)*pageSize, pageSize);
		
		//int total = this.getIweekContentBiz().getIweekcountTotal(teacher.getTeacherId()+"");
		//List list = this.getIweekContentBiz().getIweekcontent(teacher.getTeacherId()+"", (index-1)*pageSize, pageSize);
		request.setAttribute("currPage", index);
		request.setAttribute("pageAmount", (int) Math.ceil(total/new Double(pageSize)));
		request.setAttribute("pageSize", pageSize);
		
		request.setAttribute("contents", list);
		return mapping.findForward("gebiezhou");
	}
	/**
	 * 用于编辑
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward initIWeekinfoEdit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
			String targetId = request.getParameter("targetId");
			String month = request.getParameter("month");
			
			request.setAttribute("targetId", targetId);
			request.setAttribute("month", month);
			return new ActionForward("/jsp/mubiaozhiding/edit_gebiezhouzhuti.jsp");
	}
	/**
	 * 目标编辑时 直接跳转
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward editIWeekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		
		return new ActionForward("/beike/iweekinfo.do?method=getIWeekinfo");
	}
	/**
	 * 更新记录
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateWeekinfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		
		return mapping.findForward("addgebiezhouzhuti");
	}
	
}
